home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / rpclib / svc_simple.c < prev    next >
C/C++ Source or Header  |  1994-03-09  |  4KB  |  155 lines

  1. /*
  2.  * $Id: svc_simple.c,v 1.3 1994/02/25 00:21:41 jraja Exp $
  3.  *
  4.  * $Log: svc_simple.c,v $
  5.  * Revision 1.3  1994/02/25  00:21:41  jraja
  6.  * Changed malloc, calloc and free to mem_alloc, mem_calloc and mem_free,
  7.  * respectively.
  8.  *
  9.  * Revision 1.2  1993/11/10  02:53:00  jraja
  10.  * Fixed includes. ANSI prototypes.
  11.  *
  12.  */
  13. /* @(#)svc_simple.c    2.2 88/08/01 4.0 RPCSRC */
  14. /*
  15.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  16.  * unrestricted use provided that this legend is included on all tape
  17.  * media and as a part of the software program in whole or part.  Users
  18.  * may copy or modify Sun RPC without charge, but are not authorized
  19.  * to license or distribute it to anyone else except as part of a product or
  20.  * program developed by the user.
  21.  * 
  22.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  23.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  24.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  25.  * 
  26.  * Sun RPC is provided with no support and without any obligation on the
  27.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  28.  * modification or enhancement.
  29.  * 
  30.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  31.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  32.  * OR ANY PART THEREOF.
  33.  * 
  34.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  35.  * or profits or other special, indirect and consequential damages, even if
  36.  * Sun has been advised of the possibility of such damages.
  37.  * 
  38.  * Sun Microsystems, Inc.
  39.  * 2550 Garcia Avenue
  40.  * Mountain View, California  94043
  41.  */
  42. #if !defined(lint) && defined(SCCSIDS)
  43. static char sccsid[] = "@(#)svc_simple.c 1.18 87/08/11 Copyr 1984 Sun Micro";
  44. #endif
  45.  
  46. /* 
  47.  * svc_simple.c
  48.  * Simplified front end to rpc.
  49.  *
  50.  * Copyright (C) 1984, Sun Microsystems, Inc.
  51.  */
  52.  
  53. #include <sys/param.h>
  54. #include <stdio.h>
  55. #include <rpc/rpc.h>
  56. #include <rpc/pmap_clnt.h>
  57. #include <sys/socket.h>
  58. #include <netdb.h>
  59.  
  60. static struct proglst {
  61.     char *(*p_progname)(char *);
  62.     int  p_prognum;
  63.     int  p_procnum;
  64.     xdrproc_t p_inproc, p_outproc;
  65.     struct proglst *p_nxt;
  66. } *proglst;
  67. static void universal(struct svc_req *rqstp, SVCXPRT *transp);
  68. static SVCXPRT *transp;
  69. struct proglst *pl;
  70.  
  71. registerrpc(u_long prognum, u_long versnum, u_long procnum, 
  72.         char *(*progname)(char *), xdrproc_t inproc, xdrproc_t outproc)
  73. {
  74.     
  75.     if (procnum == NULLPROC) {
  76.         (void) fprintf(stderr,
  77.             "can't reassign procedure number %d\n", NULLPROC);
  78.         return (-1);
  79.     }
  80.     if (transp == 0) {
  81.         transp = svcudp_create(RPC_ANYSOCK);
  82.         if (transp == NULL) {
  83.             (void) fprintf(stderr, "couldn't create an rpc server\n");
  84.             return (-1);
  85.         }
  86.     }
  87.     (void) pmap_unset((u_long)prognum, (u_long)versnum);
  88.     if (!svc_register(transp, (u_long)prognum, (u_long)versnum, 
  89.         universal, IPPROTO_UDP)) {
  90.             (void) fprintf(stderr, "couldn't register prog %d vers %d\n",
  91.             prognum, versnum);
  92.         return (-1);
  93.     }
  94.     pl = (struct proglst *)mem_alloc(sizeof(struct proglst));
  95.     if (pl == NULL) {
  96.         (void) fprintf(stderr, "registerrpc: out of memory\n");
  97.         return (-1);
  98.     }
  99.     pl->p_progname = progname;
  100.     pl->p_prognum = prognum;
  101.     pl->p_procnum = procnum;
  102.     pl->p_inproc = inproc;
  103.     pl->p_outproc = outproc;
  104.     pl->p_nxt = proglst;
  105.     proglst = pl;
  106.     return (0);
  107. }
  108.  
  109. static void
  110. universal(struct svc_req *rqstp, SVCXPRT *transp)
  111. {
  112.     int prog, proc;
  113.     char *outdata;
  114.     char xdrbuf[UDPMSGSIZE];
  115.     struct proglst *pl;
  116.  
  117.     /* 
  118.      * enforce "procnum 0 is echo" convention
  119.      */
  120.     if (rqstp->rq_proc == NULLPROC) {
  121.         if (svc_sendreply(transp, xdr_void, (char *)NULL) == FALSE) {
  122.             (void) fprintf(stderr, "xxx\n");
  123.             exit(1);
  124.         }
  125.         return;
  126.     }
  127.     prog = rqstp->rq_prog;
  128.     proc = rqstp->rq_proc;
  129.     for (pl = proglst; pl != NULL; pl = pl->p_nxt)
  130.         if (pl->p_prognum == prog && pl->p_procnum == proc) {
  131.             /* decode arguments into a CLEAN buffer */
  132.             bzero(xdrbuf, sizeof(xdrbuf)); /* required ! */
  133.             if (!svc_getargs(transp, pl->p_inproc, xdrbuf)) {
  134.                 svcerr_decode(transp);
  135.                 return;
  136.             }
  137.             outdata = (*(pl->p_progname))(xdrbuf);
  138.             if (outdata == NULL && pl->p_outproc != xdr_void)
  139.                 /* there was an error */
  140.                 return;
  141.             if (!svc_sendreply(transp, pl->p_outproc, outdata)) {
  142.                 (void) fprintf(stderr,
  143.                     "trouble replying to prog %d\n",
  144.                     pl->p_prognum);
  145.                 exit(1);
  146.             }
  147.             /* free the decoded arguments */
  148.             (void)svc_freeargs(transp, pl->p_inproc, xdrbuf);
  149.             return;
  150.         }
  151.     (void) fprintf(stderr, "never registered prog %d\n", prog);
  152.     exit(1);
  153. }
  154.  
  155.